#ifndef _RTSPINPUT_H_
#define _RTSPINPUT_H_

#ifdef __Win32__
#pragma warning(disable:4786)
#endif

#include <string>
#include <list>
#include <map>

#include "RTSPClient.h"
#include "OSThread.h"
#include "SdpParser.h"
#include "RTPBuffer.h"
#include "CommonOpt.h"
#include "LiveRecvFilter.h"


using namespace std;

class CRTSPInput : public OSTask  
{
	struct InterMsg 
	{
		UInt32	Type;
		UInt32  TrackID;
		Int32	Ret;
	};
	struct STrackInfo
	{
		UInt32	TimeStamp;
		UInt32	SeqNo;
	};
	
public:
	CRTSPInput();
	virtual ~CRTSPInput();

	Bool Initialize( const string& strPrgmName, string strIP ="" , UInt16 nPort =0 , CRTSPClient::EProtocol eProto=CRTSPClient::CTUDP);
	Bool Run();
	
	int	Start();
	int	Stop();
	
	int	Connect();
	int	Setup();
	int	Play(float vStartTime);
	int	Pause();
	int	Close();
	float GetRange();
	
	const char * GetSDP();

	Bool	GetRtpInfo( UInt32 vTrackID, UInt16 &vSeqNo, UInt32 &vTimeStamp );
	Int32	PreReadData();
	CRTPBuffer*	GetDataBuffer();
	UInt32	GetCurDuration();
	void	FreeData();
	Bool Teardown();

protected:	
	Bool DoMsg(UInt32 vType,UInt32 TrackID);
	void Clear();

	void Display(  );
	UInt32 GetDuration( );
	void CalcPos();

protected:
	string			m_strPrgmName;

	CRTSPClient		m_RTSPClient;
	string			m_Sdp;

	//Parameters
	UInt32			m_IP;
	UInt16			m_Port;
	
	Bool			m_IsPlaying;
	
	Int32			m_RTSPStatus;  
	
	//
	std::list< InterMsg *> m_MsgList;
	OSCond				m_BlockCond;
	OSMutex				m_BlockMutex;
	
	OSMutex		m_IsRunMutex;
	Bool		m_IsRun;
	
	char		m_ErrorMsg[100];
	Int32		m_ErrorCode;

	std::map<UInt32, STrackInfo>	m_TrackInfos;
	//key is payload type(video,audio,other)
	std::map<UInt8, UInt32>		m_TrackTypes;

	TrackMap	m_TrackList;

	OSMutex		m_DataMutex;
	CRTPBuffer		m_RTPBuffer;
	std::list<RtpPacket*>		m_DataList;
	std::list<RtpPacket*>		m_NeedFreeList;

	struct SPos
	{
		UInt32		LastTimeStamp;
		UInt32		CurPos;
		UInt32		TrackID;
		Bool		isSet;
	}m_Pos;
};

#endif
